package org.zl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.zl.pojo.ClazzInfo;
import org.zl.pojo.UserInfo;
import org.zl.service.ClazzInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/clazz")
public class ClazzInfoController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ClazzInfoService clazzInfoService;

    /**
     * 查询所有学生信息
     * @return
     */
    @RequestMapping("/u_findAllClazz")
    public String findAll(){
        List<ClazzInfo> clazzInfos=clazzInfoService.getAllClazzes();
        request.setAttribute("clazzInfos",clazzInfos);
        return "ClazzInfo/show_allclazz";
    }

    /**
     * 增加班级信息
     * @return
     */
    @RequestMapping("/c-save.action")
    public String save_1(){
        return "ClazzInfo/save_clazz";
    }

    @RequestMapping("/save-clazz.action")
    public String save_2(HttpServletResponse response) throws IOException {
        String claId=request.getParameter("claId");
        String claCode=request.getParameter("claCode");
        String claName=request.getParameter("claName");
        String clamId=request.getParameter("clamId");
        ClazzInfo clazzInfo=new ClazzInfo();
        clazzInfo.setClaId(Integer.parseInt(claId));
        clazzInfo.setClaCode(claCode);
        clazzInfo.setClaName(claName);
        clazzInfo.setClaCreatDate(new Date());
        clazzInfo.setClamId(Integer.parseInt(clamId));
        if(clazzInfoService.save(clazzInfo)){
            response.sendRedirect(request.getContextPath()+"/clazz/u_findAllClazz");
        }else {
            request.setAttribute("msg","添加班级信息失败");
            return "msg";
        }
        return null;
    }
    /**
     * 删除班级信息
     * @return
     */
    @RequestMapping("/c-delete.action")
    public String delete(HttpServletResponse response) throws IOException {
        String id=request.getParameter("id");
        if(clazzInfoService.delete(Integer.parseInt(id))){
            response.sendRedirect(request.getContextPath()+"/clazz/u_findAllClazz");
        }else{
            request.setAttribute("msg","删除班级信息失败");
            return "msg";
        }
        return null;
    }

    /**
     * 修改班级信息
     */
    @RequestMapping("/c-update.action")
    public String update(){
        String id=request.getParameter("id");
        ClazzInfo clazz=clazzInfoService.findClazz(Integer.parseInt(id));
        request.setAttribute("clazz",clazz);
        return "ClazzInfo/update_clazz";
    }

    @RequestMapping("/update-clazz.action")
    public String executeUpdate(ClazzInfo clazz,HttpServletRequest request) {

        System.out.println(clazz);
        try {
            if(clazzInfoService.update(clazz)) {
                return "redirect:/clazz/u_findAllClazz";
            }else {
                request.setAttribute("msg", "修改失败，请确认要修改的学号是否存在");
                return"msg/fail";
            }
        }catch(Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "修改失败，系统异常请与系统人员联系");
            return"msg/fail";
        }
    }
}
